<?php
/**
 * Created by PhpStorm.
 * 商品属性值
 * User: caicm
 * Date: 2017/6/17
 * Time: 17:18
 */

namespace WMApi\Model;


use Think\Model;

class WmAttrValueModel extends Model {

    /**
     * 店铺商品属性值
     * Created By caicm
     * @param int $attr_id 属性id
     * @param int $store_id 店铺id
     * @return mixed
     */
    public function get_attr_vlaue_list($attr_id=0, $store_id=0){

        $where = array(
            'attr_id' => $attr_id,
            'store_id' => array('in', array(0,$store_id)),
            'is_start' => 1,
            'is_del' => 0
        );


        $field = 'attr_value_id,attr_value';

        return $this->field($field)->where($where)->order('store_id desc,attr_id desc')->select();

    }

    /**
     * 获取属性值数量
     * Created By caicm
     * @param int $attr_id 属性id
     * @param int $store_id 店铺id
     * @param string $attr_value 属性值
     * @return mixed
     */
    public function get_attr_value_count($attr_id=0, $store_id=0, $attr_value=''){

        $where = array(
            'attr_id' => $attr_id,
            'store_id' => array('in', array(0,$store_id)),
            'attr_value' => $attr_value,
            'is_start' => 1,
            'is_del' => 0
        );

        return $this->where($where)->count('attr_value_id');

    }

    /**
     * 添加属性值
     * Created By caicm
     * @param int $attr_id 属性id
     * @param int $store_id 店铺id
     * @param string $attr_value 属性值
     * @return mixed
     */
    public function add_attr_vlaue($attr_id=0, $store_id=0, $attr_value=''){

        $data = array(
            'attr_id' => $attr_id,
            'store_id' => $store_id,
            'attr_value' => $attr_value,
            'add_time' => gmtime(),
            'is_start' => 1,
            'is_del' => 0
        );

        return $this->data($data)->add();

    }

    /**
     * 获取属性值详情
     * Created By caicm
     * @param $attr_value_id
     * @return mixed
     */
    public function get_attr_info($attr_value_id){

        $where = array(
            'v.attr_value_id' => $attr_value_id,
            'v.is_start' => 1,
            'v.is_del' => 0,
            'a.is_start' => 1,
            'a.is_del' => 0
        );

        $field = 'v.attr_value_id,v.attr_value,a.attr_id,a.attr_name';
        $join = ' left join sdx_wm_attr as a on v.attr_id=a.attr_id';

        return $this->alias('v')->field($field)->join($join)->where($where)->find();

    }



}